# -*- coding: utf-8 -*-

from osv import fields,osv
from osv.orm import browse_record
import tools
from functools import partial
import pytz
import pooler
from tools.translate import _
from service import security
import netsvc


def get_all_registered_courses(pool, cr, uid, student_id, personal=True, context=None):
    course_obj = pool.get('aas.course.course')
    course_student_obj = pool.get('aas.course.student')
    res = []
    if personal:
        registers_ids = course_student_obj.search(cr, uid, [('course_id.is_in_current_academic_state', '=', 1), 
                                                        ('student_id', '=', student_id),
                                                        ('course_id.state', 'not in', ('draft', 'closed'))], context=context)
        if registers_ids:
            sql = '''
                SELECT course_id
                FROM aas_course_student
                WHERE id in %s
            '''
            cr.execute(sql, (tuple(registers_ids + [-1, -1]),))
            if cr.rowcount > 0:
                courses_ids = [id for (id,) in cr.fetchall()]
                res = list(set(courses_ids))
    else:
        courses_ids = course_obj.search(cr, uid, [('is_in_current_academic_state', '=', 1),
                                                ('state', 'not in', ('draft', 'closed'))], context=context)
        res = list(set(courses_ids))
    return res

class aas_personal_timetable(osv.TransientModel):
    _name = 'aas.personal.timetable'
    _columns = {
        'timetable_lines': fields.many2many('aas.course.course', 'personal_timetable_course', 'timetable_id', 'course_id', string='Details', readonly=True, context={'mode': 2}),
    }
    
    def default_get(self, cr, uid, fields, context=False):
        res = super(aas_personal_timetable, self).default_get(cr, uid, fields, context=context)
        courses_ids = get_all_registered_courses(self.pool, cr, uid, uid, personal=True, context=context)
        res.update({'timetable_lines': courses_ids})
        return res
    
aas_personal_timetable()


class aas_session_timetable(osv.TransientModel):
    _name = 'aas.session.timetable'
    _columns = {
        'timetable_lines': fields.many2many('aas.course.course', 'session_timetable_course', 'timetable_id', 'course_id', string='Details', readonly=True, context={'mode': 2}),
    }
    
    def default_get(self, cr, uid, fields, context=False):
        res = super(aas_session_timetable, self).default_get(cr, uid, fields, context=context)
        courses_ids = get_all_registered_courses(self.pool, cr, uid, uid, personal=False, context=context)
        res.update({'timetable_lines': courses_ids})
        self.list_id = False
        if courses_ids:
            sql = '''
                SELECT list_id
                FROM aas_course_course
                WHERE id = %s
            '''
            cr.execute(sql, (courses_ids[0],))
            if cr.rowcount > 0:
                self.list_id = cr.fetchone()[0]
        return res
    
    def print_report(self, cr, uid, ids, context=False):
        datas = {}
        if not self.list_id:
            raise osv.except_osv(_('Thông báo!'), _("Không có thông tin thời khóa biểu. Không thể tải bản in!"))
        datas['id'] = self.list_id
        datas['ids'] = [self.list_id]
        datas['model'] = 'aas.course.list'
        return {
            'type': 'ir.actions.report.xml', 
            'report_name': 'aas_timetable_report', 
            'datas': datas
        }
    
aas_session_timetable()


class aas_personal_exam_schedule(osv.TransientModel):
    _name = 'aas.personal.exam.schedule'
    _columns = {
        'schedule_lines': fields.many2many('aas.course.course', 'personal_schedule_course', 'schedule_id', 'course_id', string='Details', readonly=True, context={'mode': 1}),
    }
    
    def default_get(self, cr, uid, fields, context=False):
        res = super(aas_personal_exam_schedule, self).default_get(cr, uid, fields, context=context)
        courses_ids = get_all_registered_courses(self.pool, cr, uid, uid, personal=True, context=context)
        res.update({'schedule_lines': courses_ids})
        return res
    
aas_personal_exam_schedule()



class aas_session_exam_schedule(osv.TransientModel):
    _name = 'aas.session.exam.schedule'
    _columns = {
        'schedule_lines': fields.many2many('aas.course.course', 'session_schedule_course', 'schedule_id', 'course_id', string='Details', readonly=True, context={'mode': 1}),
    }
    
    def default_get(self, cr, uid, fields, context=False):
        res = super(aas_session_exam_schedule, self).default_get(cr, uid, fields, context=context)
        courses_ids = get_all_registered_courses(self.pool, cr, uid, uid, personal=False, context=context)
        res.update({'schedule_lines': courses_ids})
        self.list_id = False
        if courses_ids:
            sql = '''
                SELECT list_id
                FROM aas_course_course
                WHERE id = %s
            '''
            cr.execute(sql, (courses_ids[0],))
            if cr.rowcount > 0:
                self.list_id = cr.fetchone()[0]
        return res
    
    def print_report(self, cr, uid, ids, context=False):
        datas = {}
        if not self.list_id:
            raise osv.except_osv(_('Thông báo!'), _("Không có thông tin lịch thi. Không thể tải bản in!"))
        datas['id'] = self.list_id
        datas['ids'] = [self.list_id]
        datas['model'] = 'aas.course.list'
        return {
            'type': 'ir.actions.report.xml', 
            'report_name': 'aas_exam_schedule_report', 
            'datas': datas
        }
    
aas_session_exam_schedule()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
